<?php
namespace Home\Controller;
use Think\Controller;
class GameController extends \Home\Common\BaseAdminController {
    //页面数据，服务器处于关闭状态时数据为0
    public function online(){
        $nums = $this->GetOnlineNum();
        $all  = array(
                'players' => 0,
                'count'   => 0,
                'online'  => 0,
                'web'     => 0,
                'client'  => 0,
                'list'    => array()
        );
        $online = CreateGameModel('online');
        //where 条件
        $where = array();
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));//当天开始时间
        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;//当天结束时间
        // echo $endToday;exit;
        $where['Time'] = array('between',array("$beginToday","$endToday"));
        $res = $online->field('Max(Count) count')->where($where)->find();

        $datastatistics = CreateGameModel('datastatistics');
        $allPlays = $datastatistics->field('count(PlatformID) players')->find();
        //结果集处理
        if(!empty($nums)){
            $all = $nums['content'];
            $all['players'] = $allPlays['players'];
            $all['count'] = $res['count'];
        }
        // 数据渲染
        $this->assign('users',$all);//顶部数据
        $this->display();
    }
    //测试数据上线
    public function getOnlineData() {
        $data = array();
        $now = time();
        $nowSec = $now + date('Z');
        $nowSec = (int)($nowSec / 86400);
        $nowSec = $nowSec * 86400;
        $nextDay = $nowSec + 86400;
        $data['datetime'] = date("Y-m-d H:i:s", $now);
        $data['list'] = array();
        $intervalSec = 5*60;
        if (IS_POST) {
            for ($j = 0; $j < 1; $j++) {
                if ($j != 0) {
                    $nowSec -= 86400;
                    $nextDay = $nowSec + 86400;
                }
                $onlineNum = array(
                    'name' => date("m-d", $nowSec) . '在线',
                    'pointInterval' => $intervalSec * 1000,
                    'pointStart' => $nowSec * 1000,
                    'data' => array()
                );
                $num = rand(0, 50);
                for ($i = $nowSec; $i <= $nextDay; $i += $intervalSec) {
                    $num = $num + rand(-2, 2);
                    array_push($onlineNum['data'], $num);
                }
                array_push($data['list'], $onlineNum);
            }
        }
        $this->ajaxReturn($data);
    }
    //获取历史信息/实时在线
    public function getHistoryData(){
        $time = $_POST['time'];
        $day = time();
        if(strlen($time) !== 10){
            $data = "时间格式错误";
            $this->ajaxReturn($data);
        }
        $day = $this->getDayBE($time);
        $data = array();
        $begin = $day[0];
        $beginSec = $begin + date('Z');
        $beginSec = (int)($beginSec / 86400);
        $beginSec = $beginSec * 86400;
        $end = $beginSec + 86400;
        $data['datetime'] = date("Y-m-d H:i:s",$begin);
        $data['list'] = array();
        $intervalSec = 5*60;
        if(IS_POST){
            $onlineNum = array(
                    'name' => date("m-d", $begin) . '在线',
                    'pointInterval' => $intervalSec * 1000,
                    'pointStart' => 1 * 1000,
                    'data' => array()
                );
            $online = CreateGameModel('online');
            $where = array();
            $where['Time'] = array('between',array("$day[0]","$day[1]"));
            $result = $online->field('time,count')->where($where)->select();
            $num = [];$arr = [];
            for ($i = $beginSec; $i < $end; $i += $intervalSec) {
                $arr[] = $i;
            }
            // dump($arr);
            if(count($result) == count($arr)){
                foreach ($result as $key => $value) {
                    # code...
                    $num[$key] = $value['count'];
                }
            }else{
                foreach ($arr as $key => $value) {
                    # code...
                    if($value == $result[0]['time']){
                        $index = $key+8;
                    }
                }
                // echo $index;先留着看看
                for($i=0;$i<$index;$i++){
                    $num[] = 0;
                }

                if(($index+count($result)+1) == count($arr)){
                    foreach ($result as $key => $value) {
                        # code...
                        $num[] = intval($value['count']);
                    }
                }else{
                    foreach ($result as $key => $value) {
                        # code...
                        $num[] = intval($value['count']);
                    }
                    for($i=0;$i<(count($arr)-$index-count($result));$i++){
                        $num[] = 0;
                    }
                }

            }
            $onlineNum['data'] = $num;
            array_push($data['list'], $onlineNum);
        }
        // dump($data);exit;
        if($time){
            $this->ajaxReturn($data);
        } else {
            $data = "请选择时间";
            $this->ajaxReturn($data);
        }
       
    }
    // 货币日志
    public function currencyLog(){
        $money = CreateGameModel('moneylog');
        $currency = C('currency');
        $where = ' 1=1';
        $nickname = I('name','');
        $currency_type = I('type_name','');
        if($nickname){
            $where .= " and PlayerName like '%".$nickname."%'";
        }
        if($currency_type){
            $where .= " and CurrencyName = '{$currency_type}'";
        }
        $count = $money->where($where)->count();
        $p = getPage($count,15);//每页显示条数

        $money_1 = CreateGameModel('moneylog');
        $list = $money_1->field("FROM_UNIXTIME(Time,'%Y-%m-%d %H:%i:%s') `time`,
        PlayerName `name`, CurrencyName currency_name,LogTypeText currency_desc,
        (date2-date1) data")->where($where)->order('time desc')
        ->limit($p->firstRow, $p->listRows)->select();

        $this->assign('money_type',$currency);
        $this->assign('list',$list);
        $this->assign('page',$p->show());
        $this->assign('name',$nickname);
        $this->assign('type_name',$currency_type);
        $this->display('currency');
    }

    // 物品日志
    public function itemLog(){
        $behavior = CreateGameModel('itemlog');
        $where = ' 1=1 ';
        $player_name = I('name','');//获取post传值
        $player_name = preg_replace("/(\s|\&nbsp\;|　|\xc2\xa0)/","",$player_name);
        if($player_name){
            $where .= " and PlayerName like '%".$player_name."%'";
        }
        $count = $behavior->where($where)->count();
        $p = getPage($count,15);//每页显示行数
        $behavior_1 = CreateGameModel('itemlog');
        $list = $behavior_1->field("FROM_UNIXTIME(Time,'%Y-%m-%d %H:%i:%s') `time`,
        PathWay way,ItemTID,ItemName,CurNum,PlayerName name")
        ->where($where)->order('time desc')->limit($p->firstRow, $p->listRows)->select();
        $this->assign('list',$list);
        $this->assign('page',$p->show());
        $this->assign('name',$player_name);
        $this->display('item');
    }
    // 行为日志
    public function behaviorLog(){
        $behavior = CreateGameModel('playerbehaviorlog');
        $where = ' 1=1 ';
        $player_name = I('name','');//获取post传值
        $player_name = preg_replace("/(\s|\&nbsp\;|　|\xc2\xa0)/","",$player_name);
        if($player_name){
            $where .= " and PlayerName like '%".$player_name."%'";
        }
        $count = $behavior->where($where)->count();
        $p = getPage($count,15);//每页显示行数
        $behavior_1 = CreateGameModel('playerbehaviorlog');
        $list = $behavior_1->field("FROM_UNIXTIME(Time,'%Y-%m-%d %H:%i:%s') `time`,ServerID,
        MapID mi,MapX mx,MapY my,BehaviorType type_id,BehaviorDesc,PlayerName name")
        ->where($where)->order('time desc')->limit($p->firstRow, $p->listRows)->select();
        // dump($list);
        $this->assign('list',$list);
        $this->assign('page',$p->show());
        $this->assign('name',$player_name);
        $this->display('behavior');
    }
    // 商城日志
    public function shoppingLog(){
        $shop = CreateGameModel('shopsell');
        $where = " 1=1";
        $name = I('name','');
        if($name){
            $where .=' and PlayerName like "%'.$name.'%" or ItemName like "%'.$name.'%"';
        }
        $count = $shop->where($where)->count();
        $p = getPage($count,15);
        $shop_1 = CreateGameModel('shopsell');
        $list = $shop_1->field("ItemName goods_name,SellCount num,SellTime time,PlayerName player_name,
            CurrencyName currency_name,PayMoney pay")
        ->where($where)->order('time desc')->limit($p->firstRow,$p->listRows)->select();
        $this->assign('list',$list);
        $this->assign('page',$p->show());
        $this->assign('name',$name);
        $this->display('shopping');
    }
    //获取线上人数
    function GetOnlineNum() {
        $res = array();
        $res = PostGameCUrl('192.168.0.103:8901/GetOnlineNum',array('NickName'=>''));
        return $res;
    }
    //获取任意一天的开始时间和结束时间
    function getDayBE($day) {
        return array(strtotime($day), strtotime($day)+24*3600-1);
    }
}